using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using System.Text;
using System.Runtime.InteropServices;


namespace WenXia
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class TextDrawer : Microsoft.Xna.Framework.DrawableGameComponent
    {
        
        private SpriteBatch spriteBatch;
        public static SpriteFontX fontX;
        private List<TextContainer> texts = new List<TextContainer>();
        private Color textColor = Color.White;

        public Color TextColor
        {
            get { return textColor; }
            set { textColor = value; }
        }

        internal List<TextContainer> Texts
        {
            get { return texts; }
            set { texts = value; }
        }
        
        public TextDrawer(Game game)
            : base(game)
        {
            this.DrawOrder = 1024;
            System.Drawing.Text.PrivateFontCollection colFont = new System.Drawing.Text.PrivateFontCollection();
            colFont.AddFontFile("pen.fon");
            System.Drawing.Font f = new System.Drawing.Font(colFont.Families[0], 20, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
            fontX = new SpriteFontX(f,
                            ((GameMain)Game).Graphics,
                            System.Drawing.Text.TextRenderingHint.AntiAliasGridFit);
            string words = System.IO.File.OpenText("char.txt").ReadToEnd();
            fontX.AddText(words);
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            
            base.Initialize();
        }
        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(Game.GraphicsDevice);
            
            base.LoadContent();
        }
        protected override void UnloadContent()
        {
            base.UnloadContent();
        }
        public Vector2 MeasureString(string str)
        {
            return fontX.MeasureString(str);
        }
        public override void Draw(GameTime gameTime)
        {
            spriteBatch.Begin();
            foreach (TextContainer t in this.texts)
            {
                //fontX.Draw(spriteBatch,t.Text,t.Position,textColor);
                spriteBatch.DrawStringX(fontX, t.Text, t.Position, textColor);
            }
            spriteBatch.End();
            base.Draw(gameTime);
        }
        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {

            base.Update(gameTime);
        }
    }
}
